import os, time, yaml
from datetime import date, datetime

with open('conf/settings.conf', 'r', encoding='utf-8') as f:
    setting = yaml.load(f.read(), Loader=yaml.FullLoader)

def do_export_excel(path, export_file, data_array, title_array=None, file_suffix=None):
    from openpyxl import Workbook
    from openpyxl import load_workbook
    from openpyxl.writer.excel import ExcelWriter 
    
    wb = load_workbook(f"{setting.get('template', 'template')}/{path}")
    ws = wb.active
    
    now = datetime.now()
    ws['B2'] = "{}".format(now)
    if title_array:
        idx = 3
        for i, val in enumerate(title_array) :
            ws.cell(idx, i + 1, val)
            
    idx = 4
    for array in data_array:
        for i, val in enumerate(array) :
            ws.cell(idx, i + 1, val)
        idx += 1
    try:
        os.mkdir(f"{setting.get('excelPath', 'xlsx')}/")
    except FileExistsError:
        # print("目录已存在，请输入一个新的目录路径。")
        pass
    name = f"{setting.get('excelPath', 'xlsx')}/{export_file if export_file else setting.get('excelFileName', '111')}_{str(int(time.time())) if not file_suffix else file_suffix}.xlsx"
    wb.save(name)
